<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Config;
use think\Request;
use think\helper\Time;
use think\Loader;

#网站设置#
class Site extends Common{

	public function _initialize(){
		if(request()->isGet()){

			$action= strtolower ( Request::instance()->action() );

			switch ($action) {
				case 'index':
					$this->assign('title',"站点设置");
					break;
				
			}

			$this->assign('controller',"site");
		}
	}
	
	

	public function wxmenu(){
		if(request()->isGet()){

	
			$menu=Db::name('wx_menu')->where('pid','0')->order('addtime asc')->select();

			$this->assign('list',$menu);

			foreach ($menu as $key => $v) {
	
				$menu[$key]['children']=Db::name('wx_menu')->where('pid',$v['id'])->order('addtime asc')->select();
			}
			$menu= json_encode($menu);

			$this->assign("title","微信菜单");
			$this->assign("menu",$menu);
			return $this->fetch();

		}else{
			$data=input('post.');

			//更新
			if($data['id']){
				$r=Db::name('wx_menu')->update($data);

				if($r!==fasle){
					return json("菜单修改成功~");
				}else{
					return json("菜单修改失败~");
				}
			//插入
			}else{
				if($data['pid']=='0'){
					$count=Db::name('wx_menu')->where('pid',"0")->count('id');
					if($count>3){
						return json("一级菜单最多3个~");
					}
				}

				$count=Db::name('wx_menu')->where('pid',$data['pid'])->count('id');
				if($count>5){
					return json("子菜单最多5个");
				}
				
				$r=Db::name('wx_menu')->insert($data);
				if($r!==fasle){
					return json("菜单添加成功");
				}else{
					return json("菜单添加失败~");
				}
			}
		}
	}

	//菜单删除
	public function menudel(){
		$id=input('id');
		$pid=Db::name('wx_menu')->where('id',$id)->value('pid');
		//删除子菜单
		if($pid=='0'){
			Db::name('wx_menu')->where('pid',$id)->delete();
		}
		
		$r=Db::name('wx_menu')->where('id',$id)->delete();
		
		if($r!==fasle){
			echo ("菜单删除成功~");
		}else{
			echo ("菜单删除失败~");
		}

	}
	//菜单生成
	public function menumake(){
		if(request()->isPost()){
			$data=[];

			$list=Db::name('wx_menu')->where('pid','0')->order('addtime asc')->select();
			foreach ($list as $key => $val) {
				$li=Db::name('wx_menu')->where('pid',$val['id'])->order('addtime asc')->select();
				
				$data['button'][$key]['name']=$val['name'];

				if(count($li) >0){
					foreach ($li as $k => $v) {
						$data['button'][$key]['sub_button'][$k]['type']=$v['type'];
						$data['button'][$key]['sub_button'][$k]['name']=$v['name'];

						//事件
						if($v['type']=='click'){
							$data['button'][$key]['sub_button'][$k]['key']=$v['val'];
						//连接
						}else if($v['type']=='view'){
							$data['button'][$key]['sub_button'][$k]['url']=$v['val'];
						}
						
					}
				}else{

					$data['button'][$key]['type']=$val['type'];
					if($val['type']=='click'){
						$data['button'][$key]['key']=$val['val'];
					}else if($val['type']=='view'){
						$data['button'][$key]['url']=$val['val'];
					}

				}

			}

			
			$json= urldecode(json_encode($data,JSON_UNESCAPED_UNICODE));
	
			Loader::import('wechat.weixin', EXTEND_PATH, '.class.php');
	        $wx = new \WeiXin();

	        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".cache('access_token');
			$result = $this->https_request($url, $json);
			$r=json_decode($result,true);
	 

			if($r['errcode']=="0"){
				$msg=$r['errmsg']."，菜单生成成功！";
			}else{
				$msg="错误代码：".$r['errcode'].",".$r['errmsg'];
				//echo $json;exit;
			}
			
			return json($msg);

		}
	}

	//curl
	function https_request($url,$data = null){
	    $curl = curl_init();
	    curl_setopt($curl, CURLOPT_URL, $url);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	    if (!empty($data)){
	        curl_setopt($curl, CURLOPT_POST, 1);
	        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	    }
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	    $output = curl_exec($curl);
	    curl_close($curl);
	    return $output;
	}





}